/* eslint-disable jsx-a11y/alt-text */
import { useDispatch, useSelector } from 'react-redux'
import { useHistory } from 'react-router-dom'
import { login } from '@/store/actions/login'

//import React from 'react'
// 导入card
import { Card, Form, Checkbox, Input, Button, message } from 'antd'
// 导入logo图片
import logo from '@/assets/logo.png'
import './Login.scss'
export const Login = () => {
  // 分发action用
  console.log(useDispatch())
  const dispatch = useDispatch()
  // 操作浏览器地址栏
  const history = useHistory()
  // 获取redux中的token
  const data = useSelector(state => state.login)
  console.log('获取到的token', data)
  const onFinish = async values => {
    try {
      // 发起action
      await dispatch(login(values.mobile, values.code))
      // 页面跳转
      history.replace('/home')
    } catch {
      message.warn('登录错误')
    }
  }
  return (
    <div className="login">
      <Card
        className="geek-card"
        bordered={false}
        bodyStyle={{
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center'
        }}
      >
        <img src={logo} className="login-logo" />
        <Form
          onFinish={onFinish} //校验表单成功后的回调函数
          name="basic"
          initialValues={{
            mobile: '13911111111',
            code: '246810',
            remember: true
          }} // 设置表单默认值
          validateTrigger={['onBlur', 'onChange']}
          autoComplete="off" //是否开启表单历史记忆功能
        >
          <Form.Item
            name="mobile"
            rules={[
              {
                pattern: /^1[3-9]\d{9}$/, //pattern 匹配正则表达式字段
                message: '手机号码格式不对'
              },
              { required: true, message: '请输入手机号' }
            ]}
          >
            <Input size="large" placeholder="请输入手机号" maxLength={11} />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              { len: 6, message: '验证码6个字符' },
              { required: true, message: '请输入验证码' }
            ]}
          >
            <Input size="large" placeholder="请输入验证码" maxLength={6} />
          </Form.Item>

          <Form.Item name="remember" valuePropName="checked">
            <Checkbox style={{ color: '#1890ff' }}>我已阅读并同意「用户协议」和「隐私条款」</Checkbox>
          </Form.Item>

          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              size="large"
              style={{
                width: '100%'
              }}
            >
              登 录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
